<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-11-08
 * Time: 11:30:56
 * Info:
 */

namespace app\common\model;

class ContentFlag extends TimeInit
{

    const FLAG = [
        1 => '置顶',
        2 => '热门',
        3 => '推荐',
        4 => '跳转'
    ];

    //文章关联属性操作
    public function articleFlagEdit($articleId, $flags = [])
    {
        //先删除关联
        $this->where(['aid' => $articleId])->delete();
        if (empty($flags)) {
            return;
        }
        $this->insertFlag($articleId, $flags);
    }

    //写入关联属性
    public function insertFlag($aid, $flags)
    {
        $insert = [];
        $time   = time();
        foreach ($flags as $value) {
            if ( ! $value) {
                continue;
            }
            $insert[] = [
                'aid'         => $aid,
                'flag_id'     => $value,
                'create_time' => $time,
            ];
        }
        $res = $this->strict(false)->field(true)->insertAll($insert);

        return $res;
    }

}
